//
//  CommentViewController.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/23.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "CommentViewController.h"
#import "squareCommentHeader.h"
#import "squareList.h"
#import "commentCell.h"
#import <MJRefresh.h>
@interface CommentViewController () <UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *viewFooter;

@property (nonatomic, copy) NSMutableArray *arrayData;
@end

@implementation CommentViewController
static NSString *const commentCellId = @"commentCell";
#pragma mark - viewLife
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 基本设置
    [self setupUI];
    
    // tableView的设置
    [self setupTableView];
    // 设置刷新控件
    [self setupRefresh];
}

- (void)setupTableView{
    // 创建tableView
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, GYMScreenW, GYMScreenH )];
    // 设置内边距
    self.tableView.contentInset = UIEdgeInsetsMake(64, 0, 44, 0);
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 自适应高度
    self.tableView.estimatedRowHeight = 50;
    // 加载header
    squareCommentHeader *header = [[[NSBundle mainBundle]loadNibNamed:@"squareCommentHeader" owner:nil options:nil]lastObject];
    header.topics = self.topics;
    header.height = self.topics.cellHeight;
    [header.btnTel addTarget:self action:@selector(btnTelClick) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView = header;
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"commentCell" bundle:nil] forCellReuseIdentifier:commentCellId];
    
    // 设置代理
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    // 添加到view中
    [self.view addSubview:self.tableView];
    // 将xib中的view移到最上面
    [self.view bringSubviewToFront:self.viewFooter];
    
}

/**
 * 基本设置
 */
- (void)setupUI{
    // 基本设置
    self.title = @"详细信息";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 设置导航栏
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"arrow_back" highImage:@"arrow_back" target:self action:@selector(leftBtnClick)];
}




/**
 * 设置刷新控件
 */
- (void)setupRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer.automaticallyHidden = NO;
}
#pragma mark - action

- (IBAction)btnCommentClick:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入评论" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}
- (void)leftBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)btnTelClick
{
    NSString *num = [[NSString alloc] initWithFormat:@"telprompt://15228789587"]; //而这个方法则打电话前先弹框  是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号
}

- (void)loadNewData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    });
}

- (void)loadMoreData{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    });
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSString *text = [alertView textFieldAtIndex:0].text;
        [self.arrayData addObject:text];
        [self.tableView.mj_header beginRefreshing];
    }
}


#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    commentCell *cell = [tableView dequeueReusableCellWithIdentifier:commentCellId];
    cell.lblContent.text = self.arrayData[indexPath.row];
    return cell;
}


#pragma mark - setter&&getter
- (NSMutableArray *)arrayData{
    if (!_arrayData) {
        _arrayData = [NSMutableArray array];
    }
    return _arrayData;
}

@end
